package com.zerowidth.db.op;

import com.zerowidth.db.DBHelper;
import com.zerowidth.db.base.SqlResult;
import com.zerowidth.db.bean.VersionBean;
import com.zerowidth.db.utils.IOUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class VersionDB {

    public static List<VersionBean> getNewVersionList(String appVersionName, String app_channel, String currentVersion) {
        String sql = String.format("SELECT * FROM version_info WHERE version_name ='%s' AND version_status=1 " +
                " AND latest_version_name > '%s' AND (channel = '%s' OR channel IS NULL) " +
                " ORDER BY latest_version_name", appVersionName, currentVersion, app_channel);

        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            ResultSet rs = sr.rs;

            List<VersionBean> vipEnumBeanList = new ArrayList<>();
            while (rs.next()) {
                VersionBean bean = new VersionBean();
                bean.id = rs.getInt("id");
                bean.latest_version_code = rs.getInt("latest_version_code");
                bean.latest_version_name = rs.getString("latest_version_name");
                bean.version_title = rs.getString("version_title");
                bean.version_update_list = rs.getString("version_update_list");
                bean.version_status = rs.getInt("version_status");
                bean.force_update = rs.getBoolean("force_update");
                bean.download_url = rs.getString("download_url");
                bean.web_page_url = rs.getString("web_page_url");
                bean.apk_size = rs.getInt("apk_size");
                bean.add_content = rs.getString("add_content");
                bean.fix_content = rs.getString("fix_content");
                bean.del_content = rs.getString("del_content");
                bean.publish_time = rs.getString("publish_datetime");

                vipEnumBeanList.add(bean);
            }

            return vipEnumBeanList;
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }

}
